﻿using System;
using System.Data;
using System.Collections;
using System.Text.RegularExpressions;

namespace StockPoint.Util
{
    public class WebUtil
    {
        public const string DEBUG_TABLE_START = "<table border=\"1\">";
        public const string DEBUG_TABLE_END = "</table><br>";

        private Hashtable cDebug = new Hashtable();

        public void AddDebugData(string sKey, string sValue)
        {
            this.cDebug.Add(sKey, sValue);
        }

        public string RemoveMark(string value)
        {
            return value.Replace("'", "''");
        }

        public string DisplayMark(string value)
        {
            return value.Replace("\'\'", "\'");
        }

        public string DisplayEnter(string value)
        {
            return value.Replace("\r\n", "<br />");
        }

        public string DisplayEnterAddLink(string value)
        {
            return AddLink(value).Replace("\r\n", "<br />");
        }

        public string DisplayOption(string value)
        {
            return value.Replace(",", "<br />");
        }

        public string AddLink(string value)
        {
            Regex regx = new Regex("(http|https)://([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
            MatchCollection mactches = regx.Matches(value);

            foreach (Match match in mactches)
            {
                value = value.Replace(match.Value, "<a href='" + match.Value + "' target='_blank' class='LinkUrl'>" + match.Value + "</a>");
            }

            return value;
        }

        /// <summary>
        /// Generates a random string with the given length
        /// </summary>
        /// <param name="size">Size of the string</param>
        /// <returns>Random string</returns>
        public string RandomString(int size)
        {
            string[] chars = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

            Random rnd = new Random();
            string random = string.Empty;

            for (int i = 0; i < size; i++)
            {
                random += chars[rnd.Next(0, 33)];
            }

            return random;
        }

        public static string GetNumberFromDecimal(decimal value)
        {
            string sNumberDisplay;

            if (value == decimal.Truncate(value)) // no decimal places, is a whole number
            {
                sNumberDisplay = value.ToString("N0");
            }
            else if (value * 10 == decimal.Truncate(value * 10)) // dealing with a one decimal place value
            {
                sNumberDisplay = value.ToString("N1");
            }
            else if (value * 100 == decimal.Truncate(value * 100)) // two decimal place value
            {
                sNumberDisplay = value.ToString("N2");
            }
            else if (value * 1000 == decimal.Truncate(value * 1000)) // three decimal place value
            {
                sNumberDisplay = value.ToString("N3");
            }
            else
            {
                sNumberDisplay = value.ToString(); // this is the 3 decimal place representation
            }

            return sNumberDisplay;
        }

        public static string GetStrGMT(decimal value)
        {
            string sResult = string.Empty;
            int iDecimal = 0;

            if (value == 0)
                return "0";

            if (value > 0)
                sResult = "+";

            if (value == decimal.Truncate(value))
            {
                sResult += value.ToString("N0") + ":00";
            }
            else if (value * 10 == decimal.Truncate(value * 10))
            {
                iDecimal = Convert.ToInt32(value.ToString("N1").Substring(value.ToString("N1").Length - 1));
                sResult += decimal.Truncate(value).ToString("N0") + ":" + (60 * iDecimal / 10).ToString();
            }
            else if (value * 100 == decimal.Truncate(value * 100))
            {
                iDecimal = Convert.ToInt32(value.ToString("N2").Substring(value.ToString("N2").Length - 2));
                sResult += decimal.Truncate(value).ToString("N0") + ":" + (60 * iDecimal / 100).ToString();
            }

            return sResult;
        }

        public static DateTime GetYourTime(DateTime dtDateTime, decimal dStandard, decimal dGMT)
        {
            DateTime dtResult;
            decimal iDiff = 0;

            iDiff = dStandard - dGMT;
            dtResult = dtDateTime.AddHours((double)iDiff);

            return dtResult;
        }

        public static string GetTimeDiff(DateTime dtStart, DateTime dtEnd)
        {
            if (dtStart == null || dtStart == DateTime.MinValue || dtEnd == null || dtEnd == DateTime.MinValue)
                return string.Empty;

            string sResult = string.Empty;
            TimeSpan duration = dtEnd - dtStart;

            if (duration.Days > 0)
            {
                sResult = duration.Days.ToString() + " days ";
                if (duration.Hours > 0)
                    sResult += duration.Hours.ToString() + " hours";
            }
            else if (duration.Hours > 0)
            {
                sResult = duration.Hours.ToString() + " hours ";

                if (duration.Minutes > 0)
                    sResult += duration.Minutes.ToString() + " mins";
            }
            else
            {
                sResult = duration.Minutes.ToString() + " mins";
            }

            return sResult;
        }

        public static string GetStrForAlertError(string pValue)
        {
            pValue = pValue.Replace("\'", "");
            pValue = pValue.Replace("\"", "");
            pValue = pValue.Replace("\r\n", "\\n");
            return "\\nError : " + pValue;
        }

        public static string GetStrForAlert(string pValue)
        {
            pValue = pValue.Replace("\'", "");
            pValue = pValue.Replace("\"", "");
            pValue = pValue.Replace("\r\n", "\\n");
            return pValue;
        }

        #region " Paging "

        private const string ITEM = "&nbsp;{0}&nbsp;";
        private const string ITEM_LINK = "<a href=\"javascript:Paging('{0}');\">{1}</a>";
        private const string ITEM_CUR = "&nbsp;<font color=\"#999999\">[</font>{0}<font color=\"#999999\">]</font>&nbsp;";
        private const string ITEM_BLOCK_PREV = "&lt;&lt;";
        private const string ITEM_BLOCK_NEXT = "&gt;&gt;";
        private const string ITEM_PAGE_PREV = "&lt;";
        private const string ITEM_PAGE_NEXT = "&gt;";

        public static string GetPaging(int iViewCount, int iTotalRecord, int iCurPage)
        {
            return GetPaging(iViewCount, iTotalRecord, iCurPage, ITEM, ITEM_CUR, ITEM_BLOCK_PREV, ITEM_BLOCK_NEXT, ITEM_PAGE_PREV, ITEM_PAGE_NEXT);
        }

        public static string GetPaging(int iViewCount, int iTotalRecord, int iCurPage, string sItem
            , string sItemCur, string sItemBlockPrev, string sItemBlockNext, string sItemPagePrev, string sItemPageNext)
        {
            int iPageCount = (iTotalRecord / iViewCount);
            if (iTotalRecord % iViewCount != 0) iPageCount++;

            string sPrevBlock = string.Empty;
            string sNextBlock = string.Empty;
            string sPrev = string.Empty;
            string sNext = string.Empty;
            string sPage = string.Empty;

            int iFromPage;
            int iToPage;
            int i;

            iFromPage = (Convert.ToInt32((iCurPage - 1) / 10) * 10) + 1;
            if (iPageCount > iFromPage + 9)
                iToPage = iFromPage + 9;
            else
                iToPage = iPageCount;

            // Previous 10 pages
            if (sItemBlockPrev != string.Empty && iPageCount > 10)
            {
                if (Convert.ToInt32((iCurPage - 1) / 10) > 0)
                    sPrevBlock = string.Format(ITEM_LINK, (iFromPage - 1), string.Format(ITEM, sItemBlockPrev));
                else
                    sPrevBlock = string.Format(ITEM, sItemBlockPrev);
            }

            // Previous page
            if (sItemPagePrev != string.Empty)
            {
                if (iCurPage > 1)
                    sPrev = string.Format(ITEM_LINK, (iCurPage - 1), string.Format(ITEM, sItemPagePrev));
                else
                    sPrev = string.Format(ITEM, sItemPagePrev);
            }

            // Pages
            for (i = iFromPage; i <= iToPage; i++)
            {
                if (i == iCurPage)
                    sPage += string.Format(ITEM_CUR, i);
                else
                    sPage += string.Format(ITEM_LINK, i, string.Format(ITEM, i));
            }

            // Next page
            if (sItemPageNext != string.Empty)
            {
                if (iCurPage < iPageCount)
                    sNext = string.Format(ITEM_LINK, (iCurPage + 1), string.Format(ITEM, sItemPageNext));
                else
                    sNext = string.Format(ITEM, sItemPageNext);
            }

            // Next 10 pages
            if (sItemBlockNext != string.Empty && iPageCount > 10)
            {
                if (iToPage < iPageCount)
                    sNextBlock = string.Format(ITEM_LINK, (iToPage + 1), string.Format(ITEM, sItemBlockNext));
                else
                    sNextBlock = string.Format(ITEM, sItemBlockNext);
            }

            return sPrevBlock + sPrev + sPage + sNext + sNextBlock;
        }

        #endregion
    }
}
